package com.aistar.controller;

import com.aistar.pojo.WorkExperience;
import com.aistar.service.WorkExperienceService;
import com.aistar.service.impl.WorkExperienceServiceImpl;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "WorkExperienceServlet",urlPatterns = "/workExperience")
public class WorkExperienceServlet  extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        switch (op){
            case "add" :add(req,resp);break;
            case "detail" :detail(req,resp);break;
            case "update" :update(req,resp);break;
        }
    }

    //添加新的工作经验
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer resumeId = Integer.valueOf(request.getParameter("resumeId"));

        String startWorkTimeStr = request.getParameter("startWorktime"); //年月
        String endWorkTimeStr = request.getParameter("endWorkTime");
        String companyName = request.getParameter("companyName");
        String jobPosition = request.getParameter("jobName");
        String companySize = request.getParameter("companySize");
        String deptName = request.getParameter("depatement");
        String companyType = request.getParameter("companyType");
        String jobDesc = request.getParameter("jobDescription");
        String jobType = request.getParameter("jobType");

        //数据类型转换
       Date startWorkTime =  DateUtil.string2DateYM(startWorkTimeStr);
        Date endWorkTime =  DateUtil.string2DateYM(endWorkTimeStr);

        //封装成WorkExperience 对象
        WorkExperience experience =   new WorkExperience();
        experience.setResumeId(resumeId);
        experience.setWorkStartTime(startWorkTime);
        experience.setWorkEndTime(endWorkTime);
        experience.setWorkCompanyName(companyName);
        experience.setWorkPosition(jobPosition);
        experience.setWorkCompanyScaler(companySize);
        experience.setWorkDepartment(deptName);
        experience.setWorkCompanyProperty(companyType);
        experience.setWorkDesc(jobDesc);
        experience.setWorkType(jobType);
        experience.setStatus(0);

        WorkExperienceService service = new WorkExperienceServiceImpl();
        ServerResponse serverResponse = service.save(experience);

        if(serverResponse.getStatus() ==1){
           serverResponse =  service.getLatestByResumeId(resumeId);
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));

    }
    protected void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer workExperienceId = Integer.valueOf(request.getParameter("id"));

        WorkExperienceService service = new WorkExperienceServiceImpl();
        ServerResponse serverResponse =  service.getById(workExperienceId);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer workExperienceId = Integer.valueOf(request.getParameter("workExperienceId"));

        String startWorkTimeStr = request.getParameter("startWorktime"); //年月
        String endWorkTimeStr = request.getParameter("endWorkTime");
        String companyName = request.getParameter("companyName");
        String jobPosition = request.getParameter("jobName");
        String companySize = request.getParameter("companySize");
        String deptName = request.getParameter("depatement");
        String companyType = request.getParameter("companyType");
        String jobDesc = request.getParameter("jobDescription");
        String jobType = request.getParameter("jobType");

        //数据类型转换
        Date startWorkTime =  DateUtil.string2DateYM(startWorkTimeStr);
        Date endWorkTime =  DateUtil.string2DateYM(endWorkTimeStr);

        //封装成WorkExperience 对象
        WorkExperience experience =   new WorkExperience();
        experience.setWorkExperiId(workExperienceId);
        experience.setWorkStartTime(startWorkTime);
        experience.setWorkEndTime(endWorkTime);
        experience.setWorkCompanyName(companyName);
        experience.setWorkPosition(jobPosition);
        experience.setWorkCompanyScaler(companySize);
        experience.setWorkDepartment(deptName);
        experience.setWorkCompanyProperty(companyType);
        experience.setWorkDesc(jobDesc);
        experience.setWorkType(jobType);
        experience.setStatus(0);

        WorkExperienceService service = new WorkExperienceServiceImpl();
        ServerResponse serverResponse = service.modify(experience);
        if(serverResponse.getStatus() ==1){
            serverResponse =  service.getById(workExperienceId);
        }
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("update response:" + JSON.toJSONString(serverResponse));
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }

        @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
